package org.dromara.patrol.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.patrol.domain.PatrolReport;
import org.dromara.patrol.domain.bo.*;
import org.dromara.patrol.domain.vo.CalculateScoreVo;
import org.dromara.patrol.domain.vo.PatrolReportVo;
import org.dromara.patrol.service.IPatrolReportService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 巡店管理-巡店报告
 * 前端访问路由地址为:/patrol/report
 *
 * @author LionLi
 * @date 2024-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/report")
public class PatrolReportController extends BaseController {

    private final IPatrolReportService patrolReportService;

    /**
     * 查询巡店管理-巡店报告列表
     */
//    @SaCheckPermission("patrol:report:list")
    @GetMapping("/list")
    public TableDataInfo<PatrolReportVo> list(PatrolReportQuery query, PageQuery pageQuery) {
        return patrolReportService.queryPageList(query, pageQuery);
    }

    /**
     * 导出巡店管理-巡店报告列表
     */
    @SaCheckPermission("patrol:report:export")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PatrolReportBo bo, HttpServletResponse response) {
        List<PatrolReportVo> list = patrolReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "巡店管理-巡店报告", PatrolReportVo.class, response);
    }



    /**
     * 获取巡店管理-巡店报告详细信息
     *
     * @param reportId 主键
     */
//    @SaCheckPermission("patrol:report:query")
    @GetMapping("/{reportId}")
    public R<PatrolReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long reportId) {
        return R.ok(patrolReportService.queryById(reportId));
    }

    /**
     * 新增巡店管理-巡店报告
     */
    @SaCheckPermission("patrol:report:add")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatrolReportBo bo) {
        return toAjax(patrolReportService.insertByBo(bo));
    }

    /**
     * 新增草稿箱-巡店报告
     */
    @SaCheckPermission("patrol:report:add")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("checkDraft")
    public R<Long> checkDraft(@RequestBody PatrolReportBo bo) {
        return R.ok(patrolReportService.checkDraft(bo));
    }

    /**
     * 整改巡店管理-巡店报告
     */
    @SaCheckPermission("patrol:report:edit")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/rectify")
    public R<Void> rectify(@Validated(EditGroup.class) @RequestBody PatrolRectifyBo bo) {
        return toAjax(patrolReportService.rectify(bo));
    }

    /**
     * 审核巡店管理-巡店报告
     */
    @SaCheckPermission("patrol:report:edit")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatrolApprovalBo bo) {
        return toAjax(patrolReportService.approval(bo));
    }

    /**
     * 删除巡店管理-巡店报告
     *
     * @param reportIds 主键串
     */
    @SaCheckPermission("patrol:report:remove")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] reportIds) {
        return toAjax(patrolReportService.deleteWithValidByIds(List.of(reportIds), true));
    }



    /**
     * 计算分数-巡店报告
     *
     * @param calculateScoreBo 请求体
     */
    @SaCheckPermission("patrol:report:calculateScore")
    @Log(title = "巡店管理-巡店报告", businessType = BusinessType.INSERT)
    @PostMapping("/calculateScore")
    public R<CalculateScoreVo> calculateScore(@RequestBody CalculateScoreBo calculateScoreBo) {
        CalculateScoreVo calculateScoreVo = patrolReportService.calculateScore(calculateScoreBo);
        return R.ok(calculateScoreVo);
    }

    /**
     * 任务巡店-开始巡店管理-巡店报告
     */
    @SaCheckPermission("patrol:report:add")
    @Log(title = "任务巡店-巡店管理-巡店报告", businessType = BusinessType.OTHER)
    @GetMapping("task/{taskId}")
    public R<PatrolReport> task(@PathVariable("taskId") Long taskId) {
        return R.ok(patrolReportService.createReport(taskId));
    }

    /**
     * 生成时间戳
     * @return  当前时间戳
     */
    @Log(title = "生成时间戳-巡店管理-巡店报告", businessType = BusinessType.OTHER)
    @GetMapping("generateTime")
    public R<String> generateTime() {
        long time = DateUtils.getNowDate().getTime();
        String redisKey = time + "";
        RedisUtils.setCacheObject(redisKey, time);
        return R.ok(redisKey);
    }
}
